package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiT_member;
import cn.com.api.entity.ApiU_favorite;
import cn.com.api.entity.ApiU_zan;

import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.ApiU_zanService;

import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "u_zan/")
public class ApiU_zanController {
    @Autowired
    private ApiU_zanService apiU_zanService;
    @Autowired
    private ApiT_memberService apiT_memberService;
	private static Logger logger = Logger.getLogger(ApiU_zanController.class);
	
	/**
	 * 点赞/取消点赞
	 * 
	 * @param conn_id
	 *            关联id
	 * @param status
	 *            点赞的状态（1-点赞；0-取消点赞）
	 * @param type
	 *            点赞的类型(类型 0动态， 1圈子中帖子，2广告， 3文章)
	 * @param user_id
	 *            用户id
	 * @param token
	 * 
	 * @return
	 */
	@RequestMapping(value = "addOrDelZan")
	public void addOrDelZan(HttpServletRequest request, HttpServletResponse response, String conn_id, String status,
			String user_id, String type, String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			
			Map param = new HashMap();
			param.put("conn_id", conn_id);
			param.put("zan_type", Integer.valueOf(type));
			param.put("user_id", user_id);
			apiU_zanService.deleteU_zanByMap(param);
			// 添加点赞
			if ("1".equals(status)) {
				ApiU_zan apiU_zan = new ApiU_zan();
				apiU_zan.setConn_id(Integer.valueOf(conn_id));
				apiU_zan.setZan_type(Integer.valueOf(type));
				apiU_zan.setUser_id(Integer.valueOf(user_id));
				ApiT_member t_member = this.apiT_memberService.findT_memberById(Integer.valueOf(user_id));
				apiU_zan.setAdd_time(Util.CurrentTime());
				apiU_zan.setNick_name(t_member.getC_nick());
				Integer ret = apiU_zanService.insertU_zan(apiU_zan);
				if(ret > 0)
				{
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作完成");
					map.put(Utils.DATAS, apiU_zan);
				}else {
					map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
					map.put(Utils.MESSAGE, "参数异常");
				}
			}else {
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成");
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 * 获取点赞数
	 * 
	 * @param conn_id
	 *            关联id
	 *            
	 * @param type
	 *            点赞的类型(类型 0 社群， 1特卖会， 2新发现， 3文章分类， 4广告 5文章)
	 * @param user_id
	 *            用户id
	 * @param token
	 * 
	 * @return
	 */
	@RequestMapping(value = "getZanCount")
	public void getZanCount(HttpServletRequest request, HttpServletResponse response, String conn_id,
			String user_id, String type, String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			
			
//			// 添加点赞
//			if ("1".equals(status)) {
//				ApiU_zan apiU_zan = new ApiU_zan();
//				apiU_zan.setConn_id(Integer.valueOf(conn_id));
//				apiU_zan.setZan_type(Integer.valueOf(type));
//				apiU_zan.setUser_id(Integer.valueOf(user_id));
//				apiU_zan.setAdd_time(Util.CurrentTime());
//				apiU_zanService.insertU_zan(apiU_zan);
//			} else {
//				Map param = new HashMap();
//				param.put("conn_id", conn_id);
//				param.put("zan_type", Integer.valueOf(type));
//				param.put("user_id", user_id);
//				apiU_zanService.deleteU_zanByMap(param);
//			}
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作完成");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
}


